package com.sleep.drug_traceability.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sleep.drug_traceability.dao.mapper.UserMapper;
import com.sleep.drug_traceability.dao.pojo.User;
import com.sleep.drug_traceability.service.LoginService;
import com.sleep.drug_traceability.service.PerfectService;
import com.sleep.drug_traceability.service.UserService;
import com.sleep.drug_traceability.vo.ErrorCode;
import com.sleep.drug_traceability.vo.Result;
import com.sleep.drug_traceability.vo.UserTableVo;
import com.sleep.drug_traceability.vo.UserVo;
import com.sleep.drug_traceability.vo.params.OneParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sleep.drug_traceability.service.impl.LoginServiceImpl.SALT;

/**
 * @author Sleep-6000
 * @date 2022/4/23
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginService loginService;
    @Autowired
    private PerfectService perfectService;

    @Override
    public User findUser(String username, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        queryWrapper.eq(User::getPwd, password);
        queryWrapper.last(" limit 1");
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        queryWrapper.last(" limit 1");
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(User user) {
        //默认生成的 id 是分布式 id 采用了雪花算法
        userMapper.insert(user);
    }

    @Override
    public UserVo getCurrentUser(String token) {
        User user = loginService.checkToken(token);
        if (user == null) {
            return null;
        }
        Integer type = user.getType();
        UserVo userVo = new UserVo();
        userVo.setId(String.valueOf(user.getId()));
        userVo.setUsername(user.getUsername());
        userVo.setType(type);
        userVo.setPhoneNumber(user.getPhoneNumber());

        if (type == 1) {
            String manName = perfectService.getManName(user.getId());
            userVo.setBindName(manName);
        } else if (type == 2) {
            String selName = perfectService.getSelName(user.getId());
            userVo.setBindName(selName);
        } else if (type == 3) {
            String supName = perfectService.getSupName(user.getId());
            userVo.setBindName(supName);
        }

        return userVo;
    }

    @Override
    public void updateIsPerfect(User user) {
        User updateUser = new User();
        updateUser.setIsPerfect(1);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, user.getId());
        userMapper.update(updateUser, updateWrapper);
    }

    @Override
    public void changePwd(Long id, String newPwd) {
        String pwd = DigestUtils.md5Hex(newPwd + SALT);
        User updateUser = new User();
        updateUser.setPwd(pwd);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, id);
        userMapper.update(updateUser, updateWrapper);
    }

    @Override
    public void changePho(Long id, String newPhone) {
        User updateUser = new User();
        updateUser.setPhoneNumber(newPhone);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, id);
        userMapper.update(updateUser, updateWrapper);
    }

    @Override
    public Result getUserTable() {
        List<UserTableVo> manList = userMapper.getManUser();
        List<UserTableVo> selList = userMapper.getSelUser();
        manList.addAll(selList);
        for (UserTableVo user : manList) {
            if ("1".equals(user.getType())) {
                user.setType("药品生产商");
            } else if ("2".equals(user.getType())) {
                user.setType("销售商");
            }
            if ("0".equals(user.getUserStatus())) {
                user.setUserStatus("禁用");
            } else if ("1".equals(user.getUserStatus())) {
                user.setUserStatus("启用");
            }
        }
        return Result.success(manList);
    }

    @Override
    public Result enable(OneParam param) {
        String username = param.getParam();
        if (StringUtils.isBlank(username)) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        User user = new User();
        user.setUserStatus(1);
        int status = 0;
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getUserStatus, status);
        wrapper.last("limit 1");
        userMapper.update(user, wrapper);
        return Result.success("change success");
    }

    @Override
    public Result disable(OneParam param) {
        String username = param.getParam();
        if (StringUtils.isBlank(username)) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        User user = new User();
        user.setUserStatus(0);
        int status = 1;
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getUserStatus, status);
        wrapper.last("limit 1");
        userMapper.update(user, wrapper);
        return Result.success("change success");
    }
}
